/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.commons.struts2extension.result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 当业务增、删、改后，进行逻辑跳转的result
 * 有3个参数：
 * transferUrl：跳转哪个页面进行处理，默认是 /admin/transfer.jsp
 * forwardUrl：进入transferUrl后完成逻辑处理后，然后再跳转的页面
 * method：进入transferUrl后，默认根据actionName进行通配符匹配 _add*,_edit*,_remove*，但是若逻辑复杂，
 * 		  需要自己来做处理，则可以传入需要处理的js方法名
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
public class TransferResult extends StrutsResultSupport {
	
	private static final long serialVersionUID = -8047605355564163075L;
	
	private String transferUrl;
	private String forwardUrl;
	private String method;

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getTransferUrl() {
		return transferUrl;
	}

	public void setTransferUrl(String transferUrl) {
		this.transferUrl = transferUrl;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	protected void doExecute(String finalLocation, ActionInvocation ai) throws Exception {
		ValueStack stack = ai.getStack();
		
		String fu = (String)stack.findValue("forwardUrl");
		if(fu==null||fu.equals("")){
			if(forwardUrl!=null&&!forwardUrl.equals(""))
				stack.setValue("forwardUrl", forwardUrl);
		}
		
		if(method!=null&&!method.equals(""))
			stack.setValue("method", method);
		
		if(transferUrl==null||transferUrl.equals("")){
			transferUrl = this.getLastFinalLocation();
		}
		
		if(transferUrl==null||transferUrl.equals("")){
			transferUrl = "/admin/transfer.jsp";
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		request.getRequestDispatcher(transferUrl).forward(request, response);
	}

}
